package org.sparkle.web.context;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.sparkle.db.basic.ServiceManager;
import org.sparkle.db.company.CompanyService;
import org.sparkle.db.company.CompanyVO;
import org.sparkle.ioc.Bean;
import org.sparkle.ioc.ServiceLookup;
import org.sparkle.register.RegisterFactory;

public class ContextListener implements ServletContextListener {

	@Override
	public void contextDestroyed(ServletContextEvent arg0) {
		// TODO Auto-generated method stub
	}

	@Override
	public void contextInitialized(ServletContextEvent arg0) {
		
		RegisterFactory.init();
		
		CompanyService cs=(CompanyService) ServiceLookup.getInstance().get("company-service");
		CompanyVO root=cs.getByName("ROOT");
		
		if(root==null){
			root=new CompanyVO();
			root.setHostname("localhost(:[0-9]*)");
			root.setName("ROOT");
			root.setLstUpdUser("root");
			try {
				//serviceManger.getCompanyService().saveAndCreateStructure(root);
				cs.save(root);
			} catch (Exception e) {				
				e.printStackTrace();
				System.exit(0);
			}
		}
	}

}
